/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import DAO.DAO_Administrador;
import Interfaces.Inter_Administrador;
import Model.Administrador;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @Ially H.R. de Andrade
 */
public class ServletAdministrador extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
  
            
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception 
 {
    
       Inter_Administrador dao =new DAO_Administrador();
        Administrador administrador = new Administrador();

        String cmd =request.getParameter("cmd");
        
       if(cmd==null){
          cmd ="erro";
      }
       
       if(cmd!=null && !cmd.equals("erro"))
      {
          try{
              administrador.setNome(request.getParameter("txtnome"));
              }
           catch(Exception e){
              throw new Exception("Erro em Administrador classe ServletAdministrador \n"+e.getMessage());
          }
       }
       
       try
        {
          RequestDispatcher rd =null;

          if(cmd.equals("listar")) //Mostrar usuarios
          {
              List administradorList = dao.ListaAdministrador();
              request.setAttribute("administradorList",administradorList);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("gravar"))
          {
              dao.GravaAdministrador(administrador);
              rd =request.getRequestDispatcher("/ServletAdministrador?cmd=listar");
          }

          else if(cmd.equals("excluir"))
          {
              dao.ExcluiAdministrador(administrador);
              rd =request.getRequestDispatcher("/ServletAdministrador?cmd=listar");
          }

          else if(cmd.equals("alterar"))
          {
              administrador =dao.LocalizaAdministrador(administrador.getId_Administrador());
              HttpSession session = request.getSession(true);
              session.setAttribute("admistrador",administrador);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("atualizar"))
          {
              dao.AlteraAdministrador(administrador);
              rd =request.getRequestDispatcher("/ServletAdministrador?cmd =listar");
          }
          else if( cmd.equalsIgnoreCase( "principal" ) )
          {
                rd = request.getRequestDispatcher( "/index.jsp" );
          }
            rd.forward(request, response);
      }
      catch(Exception e)
        {
            e.printStackTrace( );
            throw new ServletException( e );
        }
    }
  @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)    
      throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletAdministrador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletAdministrador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
